package cn.huiyunche.admin.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.huiyunche.base.service.interfaces.BAreaService;
import cn.huiyunche.base.service.interfaces.CompanyService;
import cn.huiyunche.base.service.model.BArea;
import cn.huiyunche.base.service.model.BCompany;
import cn.huiyunche.base.service.model.SUser;
import cn.huiyunche.base.service.vo.CompanyUserVo;
import cn.huiyunche.base.service.vo.PageVo;
import cn.huiyunche.base.service.vo.Result;
import cn.huiyunche.base.service.vo.SUserRequestVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;

@RequestMapping("/company")
@Controller
public class CompanyController {

	@Autowired
	private CompanyService companyService = null;
	
	@Autowired
	private BAreaService areaService = null;

	public CompanyService getCompanyService() {
		return this.companyService;
	}
	
	private BAreaService getAreaService(){
		return this.areaService;
	}

	/**
	 * 查询企业列表 
	 * @param page
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> list(PageVo page) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getCompanyService().list(page));
		return result;
	}

	/**
	 * 根据企业ID 查询企业详情
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> companyView(@PathVariable("id") String id) throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getCompanyService().selectById(Long.valueOf(id)));
		return result;
	}

	/**
	 * 根据省编码查询所属城市
	 * 
	 * @param provinceCode
	 *            省编码
	 * @return List<BArea>
	 * @throws Exception 
	 */
	@RequestMapping(value = "/select/{proCode}", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> selectCity(@PathVariable("proCode") String proCode) throws Exception {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		List<BArea> cityDate = this.getAreaService().selectCityByProvinceCode(proCode);
		result.setData(cityDate);
		return result;
	}

	/**
	 * 添加公司信息
	 * 		托运企业
	 * 		物流企业
	 * @param companyData
	 * @return
	 * @throws BusinessException
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> saveCompany(BCompany companyData) throws BusinessException {
		return this.getCompanyService().saveCompany(companyData);
	}

	/**
	 * 添加企业用户
	 * @param request
	 * @param surv
	 * @param br
	 * @return
	 */
	@RequestMapping(value = "/sign", method = RequestMethod.POST)
	@ResponseBody
	public Result<String> signCommpanyUser(HttpServletRequest request, @Valid SUserRequestVo surv, BindingResult br) {
		Result<String> result = new Result<String>(true, "注册成功!");
		if (br.hasErrors()) {
			List<ObjectError> list = br.getAllErrors();
			result.setSuccess(false);
			result.setMessage(list.get(0).getDefaultMessage());
			return result;
		}
		return this.getCompanyService().signCommpanyUser(surv);
	}

	@RequestMapping(value = "/query", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> addCompany() throws BusinessException {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getCompanyService().addCompany());
		return result;
	}

	@RequestMapping(value = "/selectcompanyuser", method = RequestMethod.GET)
	@ResponseBody
	public Result<Map<String, Object>> selectCompanyUser(PageVo page, CompanyUserVo vo) {
		Result<Map<String, Object>> result = new Result<Map<String, Object>>(true, null, "数据加载成功");
		result.setData(this.getCompanyService().selectCompanyUser(page, vo));
		return result;
	}

	@RequestMapping(value = "/selectcompany", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> selectCompany() {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getCompanyService().selectCompany());
		return result;
	}
	
	/**
	 * 根据类型 查询所有公司信息
	 * @param category
	 * @return
	 */
	@RequestMapping(value = "/selbycategory/{companyCategory}", method = RequestMethod.GET)
	@ResponseBody
	public Result<Object> selectCompanyByCategory(@PathVariable String companyCategory) {
		Result<Object> result = new Result<Object>(true, null, "数据加载成功");
		result.setData(this.getCompanyService().selectCompanyByCategory(companyCategory));
		return result;
	}

	@RequestMapping(value = "/carrier", method = RequestMethod.GET)
	@ResponseBody
	public Result<List<SUser>> carrier(Boolean truckType) throws Exception {
		Result<List<SUser>> result = new Result<List<SUser>>(true, null, "加载承运商数据成功");
		result.setData(this.getCompanyService().carrierList(truckType));
		return result;
	}

}